package com.ruoyi.yanjing.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.yanjing.common.DateUtil;
import com.ruoyi.yanjing.domain.req.LineDevicePointTemperatureReq;
import com.ruoyi.yanjing.domain.vo.DeviceTemperatureArrayVo;
import com.ruoyi.yanjing.domain.vo.LineDevicePointTemperatureVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.yanjing.mapper.DeviceTemperatureMapper;
import com.ruoyi.yanjing.domain.DeviceTemperature;
import com.ruoyi.yanjing.service.IDeviceTemperatureService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 温度日志Service业务层处理
 * 
 * @author whz
 * @date 2024-11-18
 */
@Service
public class DeviceTemperatureServiceImpl implements IDeviceTemperatureService 
{
    @Autowired
    private DeviceTemperatureMapper deviceTemperatureMapper;

    /**
     * 查询温度日志
     * 
     * @param id 温度日志主键
     * @return 温度日志
     */
    @Override
    public DeviceTemperature selectDeviceTemperatureById(String id)
    {
        return deviceTemperatureMapper.selectDeviceTemperatureById(id);
    }

    /**
     * 查询温度日志列表
     * 
     * @param deviceTemperature 温度日志
     * @return 温度日志
     */
    @Override
    public List<DeviceTemperature> selectDeviceTemperatureList(DeviceTemperature deviceTemperature)
    {
        return deviceTemperatureMapper.selectDeviceTemperatureList(deviceTemperature);
    }

    /**
     * 查询每个设备最新的温度
     *
     * @return 温度日志
     */
    @Override
    public List<DeviceTemperature> selectAllPointNewTemperatureList()
    {
        return deviceTemperatureMapper.selectAllPointNewTemperatureList();
    }


    @Override
    public DeviceTemperatureArrayVo selectAllTemperatureList(DeviceTemperature deviceTemperature)
    {
        DeviceTemperatureArrayVo deviceTemperatureArrayVo = new DeviceTemperatureArrayVo();
        List<DeviceTemperature> deviceTemperatures = deviceTemperatureMapper.selectAllTemperatureList(deviceTemperature);
        // 提取 reportTime 和 currentTemperature 到两个有序的数组
        String[] reportTimes = deviceTemperatures.stream()
                .map(o-> DateUtil.format(o.getReportTime(),"yyyy-MM-dd HH:mm:ss"))
                .toArray(String[]::new);
        String[] currentTemperatures = deviceTemperatures.stream()
                .map(DeviceTemperature::getCurrentTemperature)
                .toArray(String[]::new);

        deviceTemperatureArrayVo.setReportTimes(reportTimes);
        deviceTemperatureArrayVo.setCurrentTemperatures(currentTemperatures);
        return deviceTemperatureArrayVo;
    }


    /**
     * 查询温度日志列表
     *
     * @param deviceTemperature 温度日志
     * @return 温度日志
     */
    @Override
    public List<DeviceTemperature> deviceTemperatureList(DeviceTemperature deviceTemperature)
    {
        return deviceTemperatureMapper.deviceTemperatureList(deviceTemperature);
    }

    /**
     * 查询温度日志列表
     *
     * @param deviceTemperature 温度日志
     * @return 温度日志
     */
    @Override
    public List<LineDevicePointTemperatureVo> lineTemperatureList(LineDevicePointTemperatureReq deviceTemperature)
    {
        return deviceTemperatureMapper.lineTemperatureList(deviceTemperature);
    }



    /**
     * 新增温度日志
     * 
     * @param deviceTemperature 温度日志
     * @return 结果
     */
    @Override
    public int insertDeviceTemperature(DeviceTemperature deviceTemperature)
    {
        deviceTemperature.setCreateTime(DateUtils.getNowDate());
        return deviceTemperatureMapper.insertDeviceTemperature(deviceTemperature);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBatch(List<DeviceTemperature> deviceTemperatureList){
        return deviceTemperatureMapper.insertBatch(deviceTemperatureList);
    }



    /**
     * 修改温度日志
     * 
     * @param deviceTemperature 温度日志
     * @return 结果
     */
    @Override
    public int updateDeviceTemperature(DeviceTemperature deviceTemperature)
    {
        deviceTemperature.setUpdateTime(DateUtils.getNowDate());
        return deviceTemperatureMapper.updateDeviceTemperature(deviceTemperature);
    }

    /**
     * 批量删除温度日志
     * 
     * @param ids 需要删除的温度日志主键
     * @return 结果
     */
    @Override
    public int deleteDeviceTemperatureByIds(String[] ids)
    {
        return deviceTemperatureMapper.deleteDeviceTemperatureByIds(ids);
    }

    /**
     * 删除温度日志信息
     * 
     * @param id 温度日志主键
     * @return 结果
     */
    @Override
    public int deleteDeviceTemperatureById(String id)
    {
        return deviceTemperatureMapper.deleteDeviceTemperatureById(id);
    }
}
